﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FantastPlatform.Community.IService;
using FantastPlatform.Framework;
using FantastPlatform.Framework.Core;
using FantastPlatform.Community.IMode;
using FantastPlatform.Framework.Model;

namespace FantastPlatform.Community.UI.Service
{
    public partial class Regulations : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ILawsRegulationsService lrsService = ServiceFactory.CreateService<ILawsRegulationsService>();
            ConditionGroup group = new ConditionGroup();
            switch (Action)
            {
                case "Add":
                    string data = Data;
                    ILawsRegulations regulations = (ILawsRegulations)JsonToEntity<FantastPlatform.Community.Model.LawsRegulations>(RepairString(data));
                    regulations.Enable = true;
                    regulations.PoliceMan = LoginEmployee;
                    regulations.CreateDate = DateTime.Now;
                    if (regulations.Id == 0)
                    {   
                        lrsService.Create(regulations);
                        ReturnSuccessMessage("保存成功");
                    }
                    else
                    {
                        regulations.ModifyDate = DateTime.Now;
                        lrsService.Update(regulations);
                        ReturnSuccessMessage("更新成功");
                    }
                    break;

                case "Search":
                    Condition enableCondition = new Condition() { Type = typeof(bool).FullName, Field = "Enable", Value1 = true };
                    group.Conditions.Add(enableCondition);
                    int totalCount = 0;
                    IList<Sort> sorts = new List<Sort>();
                    sorts.Add(new Sort() { Field = "CreateDate", Order = Orders.Asc });
                    IList<ILawsRegulations> regulationsList = lrsService.Search(group, sorts, PageSize, SelfPage, out totalCount);
                    var obj = from n in regulationsList
                              select new
                              {
                                  Id = n.Id,
                                  Content = n.Content,
                                  CreateDate = n.CreateDate.ToString(),
                                  PoliceMan = n.PoliceMan.Name
                              };
                    ReturnList(totalCount, obj.ToList());
                    break;

                case "SearchByCondition":
                    string Searchjson = RepairString(Data);
                    regulations = JsonToEntity<FantastPlatform.Community.Model.LawsRegulations>(Searchjson);
                    enableCondition = new Condition() { Type = typeof(bool).FullName, Field = "Enable", Value1 = true };
                    Condition condition1 = new Condition() { Type = typeof(string).FullName, Field = "Content", Value1 = '%' + regulations.Name + '%',Comparison = Comparisons.Like, Relation = Relations.And };
                    group.Conditions.Add(enableCondition);
                    group.Conditions.Add(condition1);
                    sorts = new List<Sort>();
                    sorts.Add(new Sort() { Field = "CreateDate", Order = Orders.Asc });
                    IList<ILawsRegulations> regulationsList1 = lrsService.Search(group, sorts, PageSize, SelfPage, out totalCount);
                    var objregulation = (from n in regulationsList1
                                         select new
                                         {
                                             Id = n.Id,
                                             Content = n.Content,
                                             PoliceMan = n.PoliceMan.Name,
                                             CreateDate = n.CreateDate.ToString()
                                         });
                    var objList = objregulation.ToList();
                    ReturnList(objList.Count, objList);
                    break;

                case "Get":
                    regulations = lrsService.Get(Id);
                    object objregulationss = (new
                                     {
                                         Content = regulations.Content
                                     });
                    ReturnObj(objregulationss);
                    break;

                case "Delete":
                    if (string.IsNullOrEmpty(Data))
                    {
                        throw new Exception("传入的ID为空");
                    }
                    List<int> idList = new List<int>();
                    string[] idStrList = Data.Split(',');
                    foreach (string id in idStrList)
                    {
                        idList.Add(int.Parse(id));
                    }
                    lrsService.DeleteReally(idList);
                    ReturnSuccessMessage("删除成功");
                    break;
            }
        }
    }
}